package com.gufe.controller.admin.club;

import com.gufe.entity.PPTQueryParam;
import com.gufe.entity.PrecollectMember;
import com.gufe.result.PageResult;
import com.gufe.result.Result;
import com.gufe.service.admin.club.RetireMemberService;
import com.gufe.utils.PdfUtils;
import com.gufe.vo.PPTvo.ClubPersionInfoVO;
import com.gufe.vo.PPTvo.CollegeCountVO;
import com.gufe.vo.PPTvo.GradeCountVO;
import com.gufe.vo.PPTvo.MemberVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/preparatory")
@PreAuthorize("hasRole('ADMIN')")
// 管理端预征班人员管理
public class RetireMemberController {

    @Autowired
    private RetireMemberService retireMemberService;

    /**
     * 分页条件查询预征班成员信息
     * @param pptQueryParam
     * @return
     */
    @GetMapping("/members")
    public Result getMember(PPTQueryParam pptQueryParam) {
        log.info("分页条件查询预征班人员：{}",pptQueryParam);
        PageResult<MemberVO> pageResult = retireMemberService.getMembers(pptQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询预征班成员信息
     * @param id
     * @return
     */
    @GetMapping("/member/{id}")
    public Result getMemberById(@PathVariable Integer id) {
        log.info("查询预征班成员Id={} 信息",id);
        PrecollectMember precollectMember = retireMemberService.getMemberById(id);
        return Result.success(precollectMember);
    }


    /**
     * 根据ID导出预征班成员信息为Excel
     * @param id
     * @return
     */
    @GetMapping("/member/exportExcel/{id}")
    public ResponseEntity<byte[]> exportInfo(@PathVariable Integer id) throws IOException {
        log.info("导出预征班成员ID={} 信息",id);
        XSSFWorkbook workbook = retireMemberService.exportInfo(id);

        // 将 Excel 写入字节流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        // 设置 HTTP 响应头，让浏览器下载
        HttpHeaders headers = new HttpHeaders();
        // 3.1 设置正确的Content-Type
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 关键：设置二进制流类型
        headers.add("Content-Disposition", "attachment; filename*=UTF-8''" +
                URLEncoder.encode("info", StandardCharsets.UTF_8));
        return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);

    }

    @GetMapping("/member/exportPDF/{id}")
    public void exportToPDF(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
        PrecollectMember member = retireMemberService.getMemberById(id);
        PdfUtils.generatePdf(request,response,
                "贵州财经大学预征班学员申请表模版.pdf",
                member,member.getName()+"预征班报名表");
    }


    /**
     * 查询预征班每个学院的人数总计
     * @return
     */
    @GetMapping("/members/college")
    public Result getCollegeCount() {
        log.info("查询预征班每个学院的总人数");
        List<CollegeCountVO> collegeCountVOS = retireMemberService.getCollegeCount();
        return Result.success(collegeCountVOS);
    }


    /**
     * 查询预征班每个年级的总人数
     * @return
     */
    @GetMapping("/members/grade")
    public Result getGradeCount() {
        log.info("查询预征班每个年级的总人数");
        List<GradeCountVO> gradeCountVOS = retireMemberService.getGradeCount();
        return Result.success(gradeCountVOS);
    }

    //
    @GetMapping("/members/personInfo")
    public Result<ClubPersionInfoVO> getPersonInfo(Long studentId) {
        ClubPersionInfoVO clubPersionInfoVO = retireMemberService.getPersionInfo(studentId);
        return Result.success(clubPersionInfoVO);
    }



}
